<link rel="stylesheet" type="text/css" href="{{ URL::asset('public_css/base.css') }}" />
<link rel="stylesheet" href="{{URL::asset('handan/css/footer.css') }}"/>
<link rel="stylesheet" href="{{ URL::asset('recommend/css/bootstrap-grid.min.css') }}">
<link rel="stylesheet" href="{{ URL::asset('recommend/css/zoomify.min.css') }}">
<link rel="stylesheet" href="{{ URL::asset('recommend/css/recommend.css') }}">
<!-- header start -->
@include('new.newheader')
<!-- header end -->
<!-- banner start -->
<div class="rec_banner">
    <img src="{{ URL::asset('recommend/images/banner.png') }}" alt="">
    <img src="{{ URL::asset('recommend/images/guidance.png') }}" alt="" class="guidance">
</div>
<!-- banner end -->
<!-- title start -->
<div class="rec_title">
    <div class="rec_title_con">
        本周榜单
    </div>
</div>
<!-- title end -->
<!-- content start -->
<div class="rec_list">
    <div class="rec_list_con">
        <ul>
            @if($recomanalyst)
                @foreach($recomanalyst as $v)
                    <li>
                        <div>
                            <a href="{{url('/hrecommand?id=')}}{{$v->userid}}">
                                <img src="{{ $v->mphoto }}" alt="">
                            </a>
                        </div>
                        <div>
                            <span>{{$v->nickname}}<img src="{{ URL::asset('recommend/images/V.png') }}" alt="" style="vertical-align: sub;"></span>
                            <p><img src="{{ URL::asset('recommend/images/king.png') }}" alt="" style="vertical-align: middle;margin-right: 5px;">数据分析大师</p>
                            <span  onclick="wm.show('{{$v->analysids}}','{{$v->analysnames}}','{{$v->analysphotos}}','{{$v->direction}}')">一键订阅</span>
                            <p class="j_text_introduce">
                                {{$v->content}}
                                {{--<a href="javascript:;">更多</a>--}}
                            </p>
                            <p class="example">
                                @if($v->photo)
                                    @foreach(explode(',',$v->photo) as $photov)
                                        <img src="{{ $photov }}" alt="" class="img-rounded" width="140" height="105">
                                    @endforeach
                                @endif
                            </p>
                            <h1>本周推荐</h1>
                            <div class="rec_man">
                                @foreach($v->analysts as $k=>$anav)
                                    <span>
                                        <div><img src="{{ $anav->photo }}" alt="">
                                            <img src="{{ URL::asset('recommend/images/renzheng.png') }}" alt="">
                                        </div>
                                        <div>
                                            <p>{{$anav->username}}</p>
                                            <p>行情分析师</p>
                                            <span onclick="subhandle({{$anav->id}},'{{$anav->username}}',1,{{$v->fangxiang[$k]}},this,0)"><img src="{{ URL::asset('recommend/images/jia.png') }}" alt="" style="width: auto;height: auto;margin-left: 13px;margin-top: 3px;">{{$v->fangxiang[$k] == 1 ? '反向':'正向'}}</span>
                                        </div>
                                    </span>
                                @endforeach
                            </div>
                            <div class="time">
                                <span>{{date("Y-m-d H:i:s",$v->time)}}</span>
                                <span>订阅人数 {{$v->follownum}} &nbsp;&nbsp;&nbsp;
                                    <img src="{{ URL::asset('recommend/images/zan.png') }}" alt=""  @if($v->userid != Session::get('eai')['id'])onclick="upvotes({{$v->id}},this)" @endif>&nbsp;&nbsp;
                                    <span class="num">{{$v->upvotes?$v->upvotes:0}}</span>
                                </span>
                            </div>
                        </div>
                    </li>
                @endforeach
            @endif
        </ul>
    </div>
</div>
@if($recomanalyst)
    {{ $recomanalyst->links() }}
@endif
<!-- content end -->
<!-- recommend人 start-->
<!-- popup start -->
<div class="popup">
    <div class="popup_content">
        <p>当前订阅 <span>（同时订阅人数不能大于三人，请取消后再进行订阅）</span></p>
        <div class="popup_box">
            @if($subanalyst)
                @foreach($subanalyst as $subval)
                    <div data-analyst-id="{{$subval->analyst_id}}" data-analyst-fangxiang="{{$subval->direction}}">
                        <span><img src="{{ $subval->photo }}" alt=""></span>
                        <div>
                            <p class="analystname">{{$subval->username}}</p>
                            <p>行情分析师</p>
                            <p>{{$subval->direction == 1?"反向":"正向"}}</p>
                        </div>
                        <a href="javascript:;" onclick="subhandle({{$subval->analyst_id}},'{{$subval->username}}',0,{{$subval->direction}},this,0)"></a>
                    </div>
                @endforeach
                @for($i=0;$i<3-count($subanalyst);$i++)
                    <div>
                        <img src="{{ URL::asset('recommend/images/null.png') }}" alt="">
                    </div>
                @endfor
            @else
                <div>
                    <img src="{{ URL::asset('recommend/images/null.png') }}" alt="">
                </div>
            @endif
        </div>
        <p style="margin: 18px 0 14px;">等待订阅</p>
        <div class="popup_await"></div>
        <div class="button" onclick="allsubhandle()" id="allsubbtn">
            全部替换
        </div>
        <img src="{{ URL::asset('recommend/images/clonse.png') }}" alt="" class="clonse" onclick="wm.hide()">
    </div>
    <!-- 编辑替换遮挡层 -->
    <div class="pop_mask" style="width: 572px;height: 338px;background-color: rgba(0,0,0,.1);position: absolute;top: 0;bottom: 0;left: 0;right: 0;margin: auto;z-index: 1;display: none;"></div>
</div>
<!-- popup end -->
<div style="height: 100px;"></div>
<script src="{{URL::asset('public_js/jquery-3.2.1.min.js')}}"></script>
<script src="{{URL::asset('recommend/js/zoomify.min.js')}}"></script>
<script src="{{ URL::asset("recommend/js/recommendlist.js")}} "></script>
<script>
    //登录信息
    var islogin       = "<?php echo Session('eai')['tel'];?>";
    var loginuserid   = "<?php echo Session('eai')['id'];?>";
    var idsarr         = new Array();
    var namessarr      = new Array();
    var directionarr   = new Array();
    var gotofllow      = "{{url('/fllow')}}"; //订阅地址
    var upvotesurl     ="{{url('/upvotes')}}";//点赞地址
    // banner图展开收起
    $(".guidance").click(function(){
        $(".rec_banner").slideToggle("slow");
        $(".rec_title").css("margin-top","70px");
        return false;
    })
    // 个人简介查看全部
    $(function() {
        var max = 170,
            display = "...更多";
        $(".j_text_introduce").on("click", ".ckgd", function() {
            var me = this,
                    show = $(me).text() == display,
                    attr = show ? "his" : "sub",
                    name = show ? "收起" : display;
            $(this).parent().each(function() {
                $(this).html($(this).attr(attr)).append($(me).clone(true).text(name));
            });
        });
        $(".j_text_introduce").each(function() {
            var me = this,
                    html = $(me).html(),
                    text = getElementText(me),
                    length = text.length,
                    sub = text.substr(0, max);
            $(me).attr({
                his: text,
                sub: sub
            });
            if(length > max) {
                $(me).html(sub);
                $(me).append(
                        $("<a href='javascript:void(0)' class='ckgd'>".concat(display, "</a>"))
                );
            }
        });
    });
    // 点击查看全部的函数封装
    function getElementText(e) {
        var t = "";
        //如果传入的是元素，则继续遍历其子元素
        //否则假定它是一个数组
        e = e.childNodes || e;
        //遍历所有子节点
        for(var j = 0; j < e.length; j++) {
            //如果不是元素，追加其文本值
            //否则，递归遍历所有元素的子节点
            t += e[j].nodeType != 1 ? e[j].nodeValue : text(e[j].childNodes);
        }
        //返回区配的文本
        return t;
    }
    // 点击图片放大
    $('.example img').zoomify();
    // 弹窗隐藏函数
    var wm = {
        hide:function () {
            $('.popup').hide();
        },
        show:function (ids,names,photos,directions) {
            //显示一键订阅弹窗
            if(!islogin){
                alert("请先登录");
                return false;
            }else{
                idsarr       = ids.split(",");
                namessarr    = names.split(",");
                directionarr = directions.split(",");
                var photossarr   = photos.split(",");
                var html         = '';
                var clickstr     = '';
                for(var i=0;i<3;i++){
                    clickstr = "subhandle("+idsarr[i]+",'"+namessarr[i]+"',1,"+directionarr[i]+",'"+photossarr[i]+"',this,0)";
                    html += '<div><span><img src="'+photossarr[i]+'" alt=""><img src="{{ URL::asset('recommend/images/renzheng.png') }}" alt=""></span><div><p>'+namessarr[i]+'</p><p>行情分析师</p><p onclick="'+clickstr+'"><img src="{{ URL::asset('recommend/images/plus.png') }}" alt="" >';
                    if(directionarr[i] == 0){
                        html += "正向";
                    }else{
                        html += "反向";
                    }
                    html +='</p></div></div>';
                }
                $(".popup_await").html(html);
                //判断此推荐分析师里是否有自己,如果有自己不显示全部替换按钮
                if($.inArray(loginuserid,idsarr) == -1){
                    $("#allsubbtn").show();
                }else{
                    $("#allsubbtn").hide();
                }
                $('.popup').show();
            }
        }
    }
    //订阅操作 status 0:取消订阅  1：订阅
    function subhandle(analyid,analyusername,status,direction,obj,type) {
        //判断是否登录
        if(!islogin){
            alert("请先登录");
            return false;
        }
        //判断是不是订阅自己
        if(analyid == loginuserid){
            layer.msg("不能订阅自己");
            return false;
        }
        if(status == 1){
            layer.msg("订阅中。。。");
        }else{
            layer.msg("取消订阅中。。。");
        }
        $.post({
            url:gotofllow,
            data:{'analystid':analyid,'analystname':analyusername,'status':status,'direction':direction},
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            dataType: "json",
            async: false,
            success:function (res) {
                if(res.status == 0){
                    layer.msg("取消失败");
                }else if(res.status == 2){
                    layer.msg("用户未登录");
                }else if(res.status == 4){// 用户未充值,去充值页
                    layer.msg("用户未充值或者会员已过期");
                }else if(res.status == 3){// 已订阅3人
                    layer.msg("订阅人数不能超过3人");
                }else{
                    if(status == 1) { //用户为订阅操作
                        if(type == 1){ //用户是全部替换操作
                            return true;
                        }else{
                            layer.msg("操作成功");
                            window.location.reload();
                        }
                    }else{
                        layer.msg("操作成功");
                        $(obj).parent().removeAttr("data-analyst-id");
                        $(obj).parent().html('<img src="{{ URL::asset('recommend/images/null.png') }}" alt="">');
                        if(type == 1){ //用户是全部替换操作
                            return 1;
                        }else{
                            return 0;
                        }
                    }
                }
            }
        })
    }
    //全部订阅
    function allsubhandle() {
        $('.pop_mask').css('display','block')
        layer.msg("全部替换中");
        setTimeout(function () {
            //先取消订阅已经订阅的分析师
            $("div[data-analyst-id]").each(function(index){
                subhandle($(this).attr("data-analyst-id"),$(this).find(".analystname").html(),0,$(this).attr("data-analyst-fangxiang"),$(this),1);
            });
            $(".popup_box").html('<div><img src="{{ URL::asset('recommend/images/null.png') }}" alt=""></div>');
            //订阅当前用户推荐的分析师
            for(var i=0;i<3;i++){
                subhandle(idsarr[i],namessarr[i],1,directionarr[i],'',1);
            }
        }, 10);
        setTimeout(function () {
            layer.msg("全部替换成功");
        }, 500);
        setTimeout(function () {
            window.location.reload();
        }, 510);
    }
</script>
<!-- 底部 -->
@include('layout.footer')
